from rest_framework import serializers

from six_it.models import User, Enterprise, Label, Recruit, Question, Reply, Article, Spit


class LabelsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = ["id", "label_name"]


# 555
class LabelsOneSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = ["label_name"]


class QuestionsSerializer(serializers.ModelSerializer):
    labels = LabelsOneSerializer(read_only=True)

    class Meta:
        model = Question
        fields = ["id", "createtime", "labels", "reply",
                  "replyname", "replytime", "title", "useful_count",
                  "unuseful_count", "user", "visits"]


# 4444
class RecruitsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Recruit
        fields = ["id"]


class EnterpisesSerializer(serializers.ModelSerializer):
    recruits = RecruitsSerializer(read_only=True)

    class Meta:
        model = Enterprise
        fields = ["id", "name", "labels", "logo", "recruits", "summary"]


# 333
class FansSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ["id"]


class ArticlesSerializer(serializers.ModelSerializer):
    class Meta:
        model = Question
        fields = ["id", "title"]


class UserssSerializer(serializers.ModelSerializer):
    articles = ArticlesSerializer(read_only=True)
    fans = FansSerializer(read_only=True)

    class Meta:
        model = User
        fields = ["id", "username", "avatar", "articles", "fans"]


class Collected_Users_Serializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ["id"]


class Collected_Serializer(serializers.ModelSerializer):
    class Meta:
        model = Spit
        fields = ["collected"]


class Collected_Articles_Serializer(serializers.ModelSerializer):
    user = UserssSerializer(read_only=True)
    collected_users = Collected_Users_Serializer(read_only=True)
    collected = Collected_Serializer(read_only=True)

    class Meta:
        model = Article
        fields = ["id", "title", "content", "createtime",
                  "user", "collected_users", "collected", "image", "visits"]


# 11111
class UserOneSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ["id", "avatar", "username"]


class SubsSerializer(serializers.ModelSerializer):
    user = UserOneSerializer(read_only=True)

    class Meta:
        model = Reply
        fields = ["id", "content", "createtime", "useful_count",
                  "unuseful_count", "user"]


class Answer_Question_Serializer(serializers.ModelSerializer):
    subs = SubsSerializer(read_only=True, many=True)
    user = UserOneSerializer(read_only=True)

    class Meta:
        model = Reply
        fields = ["id", "content", "createtime", "useful_count", "problem",
                  "unuseful_count", "subs", "user", "parent"]


class UserSerializer(serializers.ModelSerializer):
    """用户序列化器类"""

    questions = QuestionsSerializer(required=False, many=True)
    answer_question = Answer_Question_Serializer(read_only=True,required=False, many=True)

    collected_articles = Collected_Articles_Serializer(read_only=True, required=False, many=True)
    labels = LabelsSerializer(required=False, many=True)
    enterpises = EnterpisesSerializer(read_only=True, required=False, many=True)
    username = serializers.CharField(read_only=True)

    class Meta:
        model = User
        fields = ['id', 'username', 'mobile', "realname", "birthday",
                  "sex", "avatar", "website", 'email', "city",
                  "address", "labels", "questions", "enterpises",
                  "answer_question", "collected_articles", ]



class UserPasswordSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ["password"]
        extra_kwargs = {
            'password': {'write_only': True}
        }

    def update(self, instance, validated_data):
        user = super().update(instance, validated_data)
        user.set_password(validated_data['password'])
        user.save()
        return user